package cn.com.http.httpheader;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Webspider {
	public static void main(String[] args) throws Exception {
		/*List<String> list = new ArrayList<String>();
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:\\Workspace\\prod_5.58\\java\\data\\nearfinder\\rezidor.nfd"))));
		String str = "";
		while((str = br.readLine()) != null){
			list.add(str.split("	")[1]);
		}*/
		List<String> list = Arrays.asList("LON","ROM","STO");
		String[] suppliers = {"hotelcorporatesystem"};
		String[] s2 = {"<Traveller><Age>21</Age></Traveller><Traveller><Age>5</Age></Traveller>","<Traveller><Age>21</Age></Traveller>",
				"<Traveller><Age>21</Age></Traveller><Traveller><Age>5</Age></Traveller><Traveller><Age>21</Age></Traveller>",
				"<Traveller><Age>21</Age></Traveller><Traveller><Age>21</Age></Traveller><Traveller><Age>21</Age></Traveller><Traveller><Age>21</Age></Traveller>"};
		List<String> list2 = new ArrayList<String>();
		for(int i = 0 ; i< list.size() ; i++){
			for(String strs : suppliers){
				for(String strs2 : s2){
				String content = "<CommandList><StartRoutingHotel><XmlLoginId>2D1GK4FEF8M9RZQ9</XmlLoginId><LoginId>2D1GK4FEF8M9RZQ9</LoginId><Destination><Descriptor>LON</Descriptor><Type>citycode</Type><Radius>10000</Radius></Destination><CheckinDates><DateOfSearch>01/10/2013-10:00</DateOfSearch></CheckinDates>"+
				"<CheckoutDates><DateOfSearch>02/10/2013-10:00</DateOfSearch></CheckoutDates><SupplierList><Supplier>radissonsas</Supplier>"+
				"</SupplierList><Timeout>1800</Timeout><AccommodationTypeList><AccommodationType>hotel</AccommodationType></AccommodationTypeList><RoomList><Room><Type>single</Type>"+
				"<TravellerList><Traveller><Age>21</Age></Traveller><Traveller><Age>5</Age></Traveller></TravellerList></Room></RoomList></StartRoutingHotel></CommandList>";
				int a = content.indexOf("<Descriptor>");
				int b = content.indexOf("</Descriptor>");
				content = content.substring(0,a+12) + "" + list.get(i) + content.substring(b,content.length());
				
				int c = content.indexOf("<Supplier>");
				int d = content.indexOf("</Supplier>");
				content = content.substring(0,c+10) + "" + strs + content.substring(d,content.length());
				
				int e = content.indexOf("<TravellerList>");
				int f = content.indexOf("</TravellerList>");
				content = content.substring(0,e+15) + "" + strs2 + content.substring(f,content.length());
				list2.add(content);
				}
			}
		}
		
		for(int i = 0 ; i< list2.size() ; i++){
			HttpRequest httpRequest = new HttpRequest();
			httpRequest.addHttpHeader("Host", "10.66.50.126");
			httpRequest.addHttpHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.2; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0");
			httpRequest.addHttpHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
			httpRequest.addHttpHeader("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
			httpRequest.addHttpHeader("Accept-Encoding", "gzip, deflate");
			httpRequest.addHttpHeader("Referer", "http://10.66.50.126/xmlspec/xmldemo/xmldemoindex.html");
			httpRequest.addHttpHeader("Cookie", "PHPSESSID=9c9c4aaeb214a14e81df08f9bfb528e3");
			httpRequest.addHttpHeader("Content-Type","text/xml");
			httpRequest.getMethod().set("POST");
			httpRequest.getUrl().set("/Xml");
			httpRequest.getVersion().set("HTTP/1.0");
			httpRequest.getContent().set(list2.get(i), "gb2312");
			httpRequest.addHttpHeader("Content-Length", httpRequest.getContent().length());
			httpRequest.addHttpHeader("Internet-Address", "10.66.50.20");
			
			
			Socket socket = new Socket("10.66.50.126", 8760);
			OutputStream os = null;
	
			os = socket.getOutputStream();
			os.write(httpRequest.toString().getBytes());
			System.out.println(i);
			
			/*BufferedReader reader = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			String line = "";
			while ((line = reader.readLine()) != null) {
				System.out.println(line);
			}
			reader.close();*/
			os.close();
			
			socket.close();
			Thread.sleep(2500);
		}
	}
}
